package com.aube.mis.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Transformer;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.aube.api.service.timeline.TimelineVoService;
import com.aube.api.service.timeline.goods.GoodsInitializeInfoVoService;
import com.aube.api.service.timeline.interaction.TimelineInteractionVoService;
import com.aube.api.service.timeline.mc.TimelineMCVoService;
import com.aube.constants.CameraTypeEnum;
import com.aube.constants.TimelineType;
import com.aube.mis.dto.InfoDTO;
import com.aube.mis.dto.ItemDTO;
import com.aube.mis.dto.ItemPositionDTO;
import com.aube.mis.dto.MulticameraDTO;
import com.aube.mis.dto.MulticameraVideoDTO;
import com.aube.mis.dto.QaDTO;
import com.aube.mis.dto.QaOptionDTO;
import com.aube.mis.dto.QuizDTO;
import com.aube.mis.dto.QuizOptionDTO;
import com.aube.mis.dto.TimelineBaseDTO;
import com.aube.mis.dto.TimelineHighlightDTO;
import com.aube.mis.dto.TimelineItemDTO;
import com.aube.mis.dto.TimelineItemIdentifyDTO;
import com.aube.mis.dto.TimelineItemLocationDTO;
import com.aube.mis.dto.VoteDTO;
import com.aube.mis.dto.VoteOptionDTO;
import com.aube.mis.dto.VsDTO;
import com.aube.mis.dto.VsOptionDTO;
import com.aube.mis.enums.ResponseCode;
import com.aube.mis.exception.MisException;
import com.aube.mis.service.AbstractService;
import com.aube.mis.service.ResourceService;
import com.aube.mis.service.TimelineService;
import com.aube.mis.util.JsonUtil;
import com.aube.support.ResultCode;
import com.aube.vo.resp.timeline.PointPercentVo;
import com.aube.vo.timeline.TimelineResp;
import com.aube.vo.timeline.goods.TimelineGoodsRespVo;
import com.aube.vo.timeline.goods.ViscoveryGoodsSummeryVo;
import com.aube.vo.timeline.goods.ViscoveryLocationAndTimeInfoVo;
import com.aube.vo.timeline.highlight.TimelineHighlightRespVo;
import com.aube.vo.timeline.info.TimelineExtraInfoVo;
import com.aube.vo.timeline.info.TimelineInfoRespVo;
import com.aube.vo.timeline.multicamera.TimelineExtraMulticameraVo;
import com.aube.vo.timeline.multicamera.TimelineMulticameraRespVo;
import com.aube.vo.timeline.qa.TimelineExtraQaVo;
import com.aube.vo.timeline.qa.TimelineQaRespVo;
import com.aube.vo.timeline.quiz.TimelineExtraQuizVo;
import com.aube.vo.timeline.quiz.TimelineQuizInfoVo;
import com.aube.vo.timeline.quiz.TimelineQuizRespVo;
import com.aube.vo.timeline.vote.TimelineExtraVoteVo;
import com.aube.vo.timeline.vote.TimelineVoteRespVo;
import com.aube.vo.timeline.vs.TimelineExtraVsVo;
import com.aube.vo.timeline.vs.TimelineVsRespVo;

@Service
@SuppressWarnings("unchecked")
public class TimelineServiceImpl extends AbstractService implements TimelineService {
	@Resource
	private TimelineVoService timelineRemoteService;

	@Resource
	private TimelineInteractionVoService timeLineInteractionRemoteService;

	@Resource
	private TimelineMCVoService timelineMCVoService;

	@Resource
	GoodsInitializeInfoVoService goodsInitializeInfoVoService;
	
	@Resource
	ResourceService resourceService;

	public <T extends TimelineBaseDTO> List<T> getTimelineList(String showId, String videoId, String type) {
		TimelineType timelineType = getTimelineTypeByCode(type);
		if (timelineType == null && StringUtils.isNotEmpty(type)) {
			throw new MisException(ResponseCode.PARAM_ERROR);
		}
		ResultCode<List<TimelineResp>> resp = timelineRemoteService.timeLineList(showId, videoId, timelineType);
		List<T> resultList = (List<T>) CollectionUtils.collect(resp.getRetval(), new Transformer() {

			@Override
			public T transform(Object object) {
				TimelineResp timelineResp = (TimelineResp) object;
				return convertTimelineDetail(timelineResp);
			}
		});

		return resultList;
	}

	@Override
	public void deleteTimeline(String timelineId) {
		timelineRemoteService.removeTimeline(timelineId);
	}

	@Override
	public void editVs(VsDTO vsDTO) {
		TimelineVsRespVo timelineVsRespVo = convertTimelineBaseVo(vsDTO, TimelineVsRespVo.class);
		List<TimelineExtraVsVo> timeLineExtraVsVoList = (List<TimelineExtraVsVo>) CollectionUtils.collect(vsDTO.getOptionList(), new Transformer() {

			@Override
			public TimelineExtraVsVo transform(Object object) {
				VsOptionDTO dto = (VsOptionDTO) object;
				TimelineExtraVsVo vo = new TimelineExtraVsVo();
				vo.setOptionValue(dto.getOptionValue());
				vo.setRid(dto.getOptionId());
				vo.setExtraSortNum(dto.getSortNum());
				vo.setPicurl(dto.getPicUrl());
				return vo;
			}
		});
		timelineVsRespVo.setTimelineExtraVsVoList(timeLineExtraVsVoList);
		timeLineInteractionRemoteService.saveTimelineVs(timelineVsRespVo);
	}

	@Override
	public void editQa(QaDTO qaDTO) {
		TimelineQaRespVo timelineQaRespVo = convertTimelineBaseVo(qaDTO, TimelineQaRespVo.class);
		List<TimelineExtraQaVo> timeLineExtraQaVoList = (List<TimelineExtraQaVo>) CollectionUtils.collect(qaDTO.getOptionList(), new Transformer() {

			@Override
			public TimelineExtraQaVo transform(Object object) {
				QaOptionDTO dto = (QaOptionDTO) object;
				TimelineExtraQaVo vo = new TimelineExtraQaVo();
				vo.setOptionValue(dto.getOptionValue());
				vo.setRid(dto.getOptionId());
				vo.setExtraSortNum(dto.getSortNum());
				vo.setPicurl(dto.getPicUrl());
				vo.setAnswer(dto.getIsAnswer());
				vo.setPopUpPicByClientType(convertPopupPic(dto.getMobilePopupPicUrl(), dto.getTvPopupPicUrl()));
				return vo;
			}
		});
		timelineQaRespVo.setShowAnswerMin(qaDTO.getShowAnswerSec() / 60);
		timelineQaRespVo.setShowAnswerSec(qaDTO.getShowAnswerSec() % 60);
		timelineQaRespVo.setTimelineExtraQaVoList(timeLineExtraQaVoList);
		timeLineInteractionRemoteService.saveTimelineQa(timelineQaRespVo);
	}

	@Override
	public void editVote(VoteDTO voteDTO) {
		TimelineVoteRespVo timelineVoteRespVo = convertTimelineBaseVo(voteDTO, TimelineVoteRespVo.class);
		List<TimelineExtraVoteVo> timeLineExtraVoteVoList = (List<TimelineExtraVoteVo>) CollectionUtils.collect(voteDTO.getOptionList(), new Transformer() {

			@Override
			public TimelineExtraVoteVo transform(Object object) {
				VoteOptionDTO dto = (VoteOptionDTO) object;
				TimelineExtraVoteVo vo = new TimelineExtraVoteVo();
				vo.setOptionValue(dto.getOptionValue());
				vo.setRid(dto.getOptionId());
				vo.setExtraSortNum(dto.getSortNum());
				vo.setPicurl(dto.getPicUrl());
				return vo;
			}
		});
		timelineVoteRespVo.setTimelineExtraVoteVoList(timeLineExtraVoteVoList);
		timeLineInteractionRemoteService.saveTimelineVote(timelineVoteRespVo);
	}

	@Override
	public void editInfo(InfoDTO infoDTO) {
		TimelineInfoRespVo timelineInfoRespVo = convertTimelineBaseVo(infoDTO, TimelineInfoRespVo.class);
		timelineInfoRespVo.setFragmentClick(infoDTO.getTriggerEnable());
		timelineInfoRespVo.setFragmentUrl(infoDTO.getTriggerPicUrl());
		TimelineExtraInfoVo timelineExtraInfoVo = new TimelineExtraInfoVo();
		timelineExtraInfoVo.setContent(infoDTO.getContent());
		timelineInfoRespVo.setTimelineExtraInfoVo(timelineExtraInfoVo);
		if (CollectionUtils.isNotEmpty(infoDTO.getTriggerPositionList())) {
			List<PointPercentVo> triggerPositionList = (List<PointPercentVo>) CollectionUtils.collect(infoDTO.getTriggerPositionList(), new Transformer() {

				@Override
				public PointPercentVo transform(Object object) {
					ItemPositionDTO dto = (ItemPositionDTO) object;
					PointPercentVo result = new PointPercentVo();
					result.setPointxPercent(new BigDecimal(dto.getPositionX()).setScale(4, RoundingMode.HALF_UP).doubleValue());
					result.setPointyPercent(new BigDecimal(dto.getPositionY()).setScale(4, RoundingMode.HALF_UP).doubleValue());
					return result;
				}
			});
			timelineInfoRespVo.setFragmentPointList(triggerPositionList);
		}
		timeLineInteractionRemoteService.saveTimelineInfo(timelineInfoRespVo);
	}

	@Override
	public void editQuiz(QuizDTO quizDTO) {
		TimelineQuizRespVo timelineQuizRespVo = convertTimelineBaseVo(quizDTO, TimelineQuizRespVo.class);
		List<TimelineExtraQuizVo> timelineExtraQuizVoList = (List<TimelineExtraQuizVo>) CollectionUtils.collect(quizDTO.getOptionList(), new Transformer() {

			@Override
			public TimelineExtraQuizVo transform(Object object) {
				QuizOptionDTO dto = (QuizOptionDTO) object;
				TimelineExtraQuizVo vo = new TimelineExtraQuizVo();
				vo.setOptionValue(dto.getOptionValue());
				vo.setRid(dto.getOptionId());
				vo.setExtraSortNum(dto.getSortNum());
				vo.setPicurl(dto.getPicUrl());
				vo.setAnswer(dto.getIsAnswer());
				vo.setPopUpPicByClientType(convertPopupPic(dto.getMobilePopupPicUrl(), dto.getTvPopupPicUrl()));
				return vo;
			}
		});
		TimelineQuizInfoVo timelineQuizInfoVo = new TimelineQuizInfoVo();
		timelineQuizInfoVo.setQuizEndMin(quizDTO.getShowAnswerEndSec() / 60);
		timelineQuizInfoVo.setQuizEndSec(quizDTO.getShowAnswerEndSec() % 60);
		timelineQuizInfoVo.setQuizStartMin(quizDTO.getShowAnswerStartSec() / 60);
		timelineQuizInfoVo.setQuizStartSec(quizDTO.getShowAnswerStartSec() % 60);
		timelineQuizInfoVo.setQuizYPicUrl(quizDTO.getCorrectPicUrl());
		timelineQuizInfoVo.setQuizNPicUrl(quizDTO.getWrongPicUrl());
		timelineQuizInfoVo.setQuizRelativePosition(quizDTO.getAnswerRelativePosition());
		timelineQuizInfoVo.setYesDesc(quizDTO.getCorrectDesc());
		timelineQuizInfoVo.setNoDesc(quizDTO.getWrongDesc());
		timelineQuizRespVo.setTimelineQuizInfoVo(timelineQuizInfoVo);
		timelineQuizRespVo.setTimelineExtraQuizVoList(timelineExtraQuizVoList);

		if (CollectionUtils.isNotEmpty(quizDTO.getAnswerPositionList())) {
			List<PointPercentVo> answerPositionList = (List<PointPercentVo>) CollectionUtils.collect(quizDTO.getAnswerPositionList(), new Transformer() {

				@Override
				public PointPercentVo transform(Object object) {
					ItemPositionDTO dto = (ItemPositionDTO) object;
					PointPercentVo result = new PointPercentVo();
					result.setPointxPercent(new BigDecimal(dto.getPositionX()).setScale(4, RoundingMode.HALF_UP).doubleValue());
					result.setPointyPercent(new BigDecimal(dto.getPositionY()).setScale(4, RoundingMode.HALF_UP).doubleValue());
					return result;
				}
			});
			timelineQuizInfoVo.setQuizPointList(answerPositionList);
		}

		timeLineInteractionRemoteService.saveTimelineQuiz(timelineQuizRespVo);
	}

	@Override
	public void editMainMulticamera(MulticameraDTO multicameraDTO) {
		TimelineMulticameraRespVo timelineMulticameraRespVo = convertTimelineBaseVo(multicameraDTO, TimelineMulticameraRespVo.class);
		timelineMulticameraRespVo.setPip(multicameraDTO.getPip());
		timelineMulticameraRespVo.setDefaultId(multicameraDTO.getDefaultVideoId());
		timelineMCVoService.saveBaseMc(timelineMulticameraRespVo);
		if("Y".equals(multicameraDTO.getPip()) && CollectionUtils.isNotEmpty(multicameraDTO.getVideoList())) {
			for (MulticameraVideoDTO multicameraVideoDTO : multicameraDTO.getVideoList()) {
				ResultCode<TimelineExtraMulticameraVo> result = timelineMCVoService.getMcDetailByRid(multicameraVideoDTO.getRid());
				List<PointPercentVo> positionList = (List<PointPercentVo>) CollectionUtils.collect(multicameraVideoDTO.getPositionList(), new Transformer() {

					@Override
					public PointPercentVo transform(Object object) {
						ItemPositionDTO dto = (ItemPositionDTO) object;
						PointPercentVo result = new PointPercentVo();
						result.setPointxPercent(new BigDecimal(dto.getPositionX()).setScale(4, RoundingMode.HALF_UP).doubleValue());
						result.setPointyPercent(new BigDecimal(dto.getPositionY()).setScale(4, RoundingMode.HALF_UP).doubleValue());
						return result;
					}
				});
				result.getRetval().setMulticameraPositionList(positionList);
				System.out.println(JsonUtil.toJson(result.getRetval()));
				timelineMCVoService.saveExtraMc(result.getRetval());
			}
		}
	}

	@Override
	public void editSubMulticamera(MulticameraVideoDTO multicameraVideoDTO) {
		TimelineExtraMulticameraVo vo = new TimelineExtraMulticameraVo();
		vo.setTlid(multicameraVideoDTO.getTimelineId());
		vo.setRelateVideoid(multicameraVideoDTO.getVideoId());
		vo.setRid(multicameraVideoDTO.getRid());
		vo.setCameraTypeEnum(CameraTypeEnum.getCameraTypeEnumByName(multicameraVideoDTO.getCameraType()).getRetval());
		vo.setDelayTime(multicameraVideoDTO.getDelayTime());
		vo.setVideoTitle(multicameraVideoDTO.getTitle());
		vo.setVideoDesc(multicameraVideoDTO.getDesc());
		if(CollectionUtils.isNotEmpty(multicameraVideoDTO.getPositionList())) {
			List<PointPercentVo> positionList = (List<PointPercentVo>) CollectionUtils.collect(multicameraVideoDTO.getPositionList(), new Transformer() {

				@Override
				public PointPercentVo transform(Object object) {
					ItemPositionDTO dto = (ItemPositionDTO) object;
					PointPercentVo result = new PointPercentVo();
					result.setPointxPercent(new BigDecimal(dto.getPositionX()).setScale(4, RoundingMode.HALF_UP).doubleValue());
					result.setPointyPercent(new BigDecimal(dto.getPositionY()).setScale(4, RoundingMode.HALF_UP).doubleValue());
					return result;
				}
			});
			vo.setMulticameraPositionList(positionList);
		}
		timelineMCVoService.saveExtraMc(vo);
	}

	@Override
	public MulticameraVideoDTO getSubMulticameraDetail(String rid) {
		ResultCode<TimelineExtraMulticameraVo> result = timelineMCVoService.getMcDetailByRid(rid);
		return convertMulticameraVideoDTO(result.getRetval());
	}

	@Override
	public <T extends TimelineBaseDTO> T getTimelineDetail(String timelineId, String videoId) {
		ResultCode<TimelineResp> result = timelineRemoteService.timeLineDetail(timelineId, videoId);
		TimelineResp resp = result.getRetval();
		return convertTimelineDetail(resp);
	}

	@Override
	public void deleteMulticamera(String rid) {
		timelineMCVoService.removeExtraMcByRid(rid);
	}

	@Override
	public TimelineItemIdentifyDTO getItemIdentifyData(String showId, String videoId, String itemId, String timelineId) {
		ResultCode<ViscoveryGoodsSummeryVo> goodsInitializeInfo = goodsInitializeInfoVoService.goodsInitializeInfo(showId, videoId, itemId, timelineId);
		ViscoveryGoodsSummeryVo vo = goodsInitializeInfo.getRetval();
		TimelineItemIdentifyDTO result = new TimelineItemIdentifyDTO();
		if(vo.getWidth() != null) {
			result.setWidth(vo.getWidth().doubleValue());
		}
		if(vo.getHeight() != null) {
			result.setHeight(vo.getHeight().doubleValue());
		}
		result.setItemId(vo.getGoodsid());
		result.setVideoId(vo.getVideoid());
		result.setStatus(vo.getStatus());
		if (CollectionUtils.isNotEmpty(vo.getViscoveryLocationAndTimeInfoVoList())) {
			List<TimelineItemLocationDTO> locationList = (List<TimelineItemLocationDTO>) CollectionUtils.collect(vo.getViscoveryLocationAndTimeInfoVoList(), new Transformer() {

				@Override
				public TimelineItemLocationDTO transform(Object object) {
					ViscoveryLocationAndTimeInfoVo vo = (ViscoveryLocationAndTimeInfoVo) object;
					TimelineItemLocationDTO result = convertTimelineItemLocationDTO(vo);
					return result;
				}
			});
			result.setLocationList(locationList);
		}
		return result;
	}

	@Override
	public void saveItemIdentifyData(TimelineItemLocationDTO timelineItemLocationDTO, String showId, String videoId, String itemId) {
		ViscoveryLocationAndTimeInfoVo vo = new ViscoveryLocationAndTimeInfoVo();
		vo.setRelatedid(timelineItemLocationDTO.getRelatedId());
		vo.setRid(timelineItemLocationDTO.getRid());
		PointPercentVo afterMovePoint = new PointPercentVo();
		afterMovePoint.setPointxPercent(timelineItemLocationDTO.getPosition().getPositionX());
		afterMovePoint.setPointyPercent(timelineItemLocationDTO.getPosition().getPositionY());
		vo.setAfterMovePoint(Arrays.asList(afterMovePoint));
		goodsInitializeInfoVoService.savePoint(vo, showId, videoId, itemId);
	}
	
	
	@Override
	public String editItem(TimelineItemDTO itemDTO) {
		TimelineGoodsRespVo timelineGoodsRespVo = convertTimelineBaseVo(itemDTO, TimelineGoodsRespVo.class);
		timelineGoodsRespVo.setGoodsid(itemDTO.getItemId());
		timelineGoodsRespVo.setTlTitle(resourceService.getItemDetail(itemDTO.getShowId(), itemDTO.getItemId()).getItemName());
		timelineGoodsRespVo.setPointRelatedidList(itemDTO.getRidList());
		ResultCode<String> result = timeLineInteractionRemoteService.saveTimelineGoods(timelineGoodsRespVo);
		return result.getRetval();
	}
	
	
	@Override
	public void editHighlight(TimelineHighlightDTO highlightDTO) {
		TimelineHighlightRespVo timelineHighlightRespVo = convertTimelineBaseVo(highlightDTO, TimelineHighlightRespVo.class);
		timelineHighlightRespVo.setBubbleKey(highlightDTO.getBubbleType());
		timelineHighlightRespVo.setRid(highlightDTO.getRid());
		timeLineInteractionRemoteService.saveTimelineHighlight(timelineHighlightRespVo);
	}

	
	@Override
	public void identifyItem(List<String> itemIdList, String showId, String videoId) {
		goodsInitializeInfoVoService.recognitionGoods(itemIdList, showId, videoId);
	}
	
	private <T extends TimelineBaseDTO> T convertTimelineDetail(TimelineResp resp) {
		TimelineType timelineType = resp.getTlTypeEnum();
		switch (timelineType) {
		case VS:
			TimelineVsRespVo timelineVsRespVo = (TimelineVsRespVo) resp;
			VsDTO vsDTO = convertVsDTO(timelineVsRespVo);
			return (T) vsDTO;
		case QA:
			TimelineQaRespVo timelineQaRespVo = (TimelineQaRespVo) resp;
			QaDTO qaDTO = convertQaDTO(timelineQaRespVo);
			return (T) qaDTO;
		case VOTE:
			TimelineVoteRespVo timelineVoteRespVo = (TimelineVoteRespVo) resp;
			VoteDTO voteDTO = convertVoteDTO(timelineVoteRespVo);
			return (T) voteDTO;
		case INFO:
			TimelineInfoRespVo timelineInfoRespVo = (TimelineInfoRespVo) resp;
			InfoDTO infoDTO = convertInfoDTO(timelineInfoRespVo);
			return (T) infoDTO;
		case QUIZ:
			TimelineQuizRespVo timelineQuizRespVo = (TimelineQuizRespVo) resp;
			QuizDTO quizDTO = convertQuizDTO(timelineQuizRespVo);
			return (T) quizDTO;
		case MULTICAMERA:
			TimelineMulticameraRespVo timelineMulticameraRespVo = (TimelineMulticameraRespVo) resp;
			MulticameraDTO multicameraDTO = convertMulticameraDTO(timelineMulticameraRespVo);
			return (T) multicameraDTO;
		case GOODS:
			TimelineGoodsRespVo timelineGoodsRespVo = (TimelineGoodsRespVo) resp;
			TimelineItemDTO timelineItemDTO = convertTimelineItemDTO(timelineGoodsRespVo);
			return (T) timelineItemDTO;
		case HIGHLIGHT:
			TimelineHighlightRespVo timelineHighlightRespVo = (TimelineHighlightRespVo) resp;
			TimelineHighlightDTO timelineHighlightDTO = convertTimelineHighlightDTO(timelineHighlightRespVo);
			return (T) timelineHighlightDTO;
		default:
			return (T) convertTimelineBaseDTO(resp, TimelineBaseDTO.class);
		}
	}

	private VsDTO convertVsDTO(TimelineVsRespVo timelineVsRespVo) {
		VsDTO vsDTO = convertTimelineBaseDTO(timelineVsRespVo, VsDTO.class);
		List<VsOptionDTO> optionList = (List<VsOptionDTO>) CollectionUtils.collect(timelineVsRespVo.getTimelineExtraVsVoList(), new Transformer() {

			@Override
			public VsOptionDTO transform(Object object) {
				TimelineExtraVsVo vo = (TimelineExtraVsVo) object;
				VsOptionDTO result = new VsOptionDTO();
				result.setOptionId(vo.getRid());
				result.setOptionValue(vo.getOptionValue());
				result.setSortNum(vo.getExtraSortNum());
				if (vo.getNum() == null) {
					result.setNum(0);
				} else {
					result.setNum(vo.getNum().intValue());
				}
				result.setPicUrl(vo.getPicurl());
				return result;
			}
		});
		if (timelineVsRespVo.getTotalNum() == null) {
			vsDTO.setTotalNum(0);
		} else {
			vsDTO.setTotalNum(timelineVsRespVo.getTotalNum().intValue());
		}
		vsDTO.setOptionList(optionList);
		return vsDTO;
	}

	private QaDTO convertQaDTO(TimelineQaRespVo timelineQaRespVo) {
		QaDTO qaDTO = convertTimelineBaseDTO(timelineQaRespVo, QaDTO.class);
		List<QaOptionDTO> optionList = (List<QaOptionDTO>) CollectionUtils.collect(timelineQaRespVo.getTimelineExtraQaVoList(), new Transformer() {

			@Override
			public QaOptionDTO transform(Object object) {
				TimelineExtraQaVo vo = (TimelineExtraQaVo) object;
				QaOptionDTO result = new QaOptionDTO();
				result.setOptionId(vo.getRid());
				result.setOptionValue(vo.getOptionValue());
				result.setSortNum(vo.getExtraSortNum());
				result.setIsAnswer(vo.getAnswer());
				if (vo.getNum() == null) {
					result.setNum(0);
				} else {
					result.setNum(vo.getNum().intValue());
				}
				result.setPicUrl(vo.getPicurl());
				JSONObject jsonObject = JsonUtil.json2Obj(vo.getPopUpPicByClientType(), JSONObject.class);
				if (jsonObject != null && !jsonObject.isEmpty()) {
					JSONObject mobile = jsonObject.getJSONObject("mobile");
					if (mobile != null) {
						result.setMobilePopupPicUrl(mobile.getString("picUrl"));
					}
					JSONObject tv = jsonObject.getJSONObject("tv");
					if (tv != null) {
						result.setTvPopupPicUrl(tv.getString("picUrl"));
					}
				}
				return result;
			}
		});
		if (timelineQaRespVo.getTotalNum() == null) {
			qaDTO.setTotalNum(0);
		} else {
			qaDTO.setTotalNum(timelineQaRespVo.getTotalNum().intValue());
		}
		qaDTO.setShowAnswerSec(timelineQaRespVo.gainShowAnswerTime());
		qaDTO.setOptionList(optionList);
		return qaDTO;
	}

	private VoteDTO convertVoteDTO(TimelineVoteRespVo timelineVoteRespVo) {
		VoteDTO voteDTO = convertTimelineBaseDTO(timelineVoteRespVo, VoteDTO.class);
		List<VoteOptionDTO> optionList = (List<VoteOptionDTO>) CollectionUtils.collect(timelineVoteRespVo.getTimelineExtraVoteVoList(), new Transformer() {

			@Override
			public VoteOptionDTO transform(Object object) {
				TimelineExtraVoteVo vo = (TimelineExtraVoteVo) object;
				VoteOptionDTO result = new VoteOptionDTO();
				result.setOptionId(vo.getRid());
				result.setOptionValue(vo.getOptionValue());
				result.setSortNum(vo.getExtraSortNum());
				if (vo.getNum() == null) {
					result.setNum(0);
				} else {
					result.setNum(vo.getNum().intValue());
				}
				result.setPicUrl(vo.getPicurl());
				return result;
			}
		});
		if (timelineVoteRespVo.getTotalNum() == null) {
			voteDTO.setTotalNum(0);
		} else {
			voteDTO.setTotalNum(timelineVoteRespVo.getTotalNum().intValue());
		}
		voteDTO.setOptionList(optionList);
		return voteDTO;
	}

	private InfoDTO convertInfoDTO(TimelineInfoRespVo timelineInfoRespVo) {
		InfoDTO infoDTO = convertTimelineBaseDTO(timelineInfoRespVo, InfoDTO.class);
		infoDTO.setContent(timelineInfoRespVo.getTimelineExtraInfoVo().getContent());
		infoDTO.setTriggerEnable(timelineInfoRespVo.getFragmentClick());
		infoDTO.setTriggerPicUrl(timelineInfoRespVo.getFragmentUrl());
		if (CollectionUtils.isNotEmpty(timelineInfoRespVo.getFragmentPointList())) {
			List<ItemPositionDTO> triggerPositionList = (List<ItemPositionDTO>) CollectionUtils.collect(timelineInfoRespVo.getFragmentPointList(), new Transformer() {

				@Override
				public ItemPositionDTO transform(Object object) {
					PointPercentVo vo = (PointPercentVo) object;
					ItemPositionDTO result = new ItemPositionDTO();
					result.setPositionX(vo.getPointxPercent());
					result.setPositionY(vo.getPointyPercent());
					return result;
				}
			});
			infoDTO.setTriggerPositionList(triggerPositionList);
		}
		return infoDTO;
	}

	private QuizDTO convertQuizDTO(TimelineQuizRespVo timelineQuizRespVo) {
		QuizDTO quizDTO = convertTimelineBaseDTO(timelineQuizRespVo, QuizDTO.class);
		List<QuizOptionDTO> optionList = (List<QuizOptionDTO>) CollectionUtils.collect(timelineQuizRespVo.getTimelineExtraQuizVoList(), new Transformer() {

			@Override
			public QuizOptionDTO transform(Object object) {
				TimelineExtraQuizVo vo = (TimelineExtraQuizVo) object;
				QuizOptionDTO result = new QuizOptionDTO();
				result.setOptionId(vo.getRid());
				result.setOptionValue(vo.getOptionValue());
				result.setSortNum(vo.getExtraSortNum());
				if (vo.getNum() == null) {
					result.setNum(0);
				} else {
					result.setNum(vo.getNum().intValue());
				}
				result.setPicUrl(vo.getPicurl());
				result.setIsAnswer(vo.getAnswer());
				result.setPicUrl(vo.getPicurl());
				JSONObject jsonObject = JsonUtil.json2Obj(vo.getPopUpPicByClientType(), JSONObject.class);
				if (jsonObject != null && !jsonObject.isEmpty()) {
					JSONObject mobile = jsonObject.getJSONObject("mobile");
					if (mobile != null) {
						result.setMobilePopupPicUrl(mobile.getString("picUrl"));
					}
					JSONObject tv = jsonObject.getJSONObject("tv");
					if (tv != null) {
						result.setTvPopupPicUrl(tv.getString("picUrl"));
					}
				}
				return result;
			}
		});
		for (int i = 0; i < optionList.size(); i++) {
			QuizOptionDTO quizOptionDTO = optionList.get(i);
			if("Y".equals(quizOptionDTO.getIsAnswer())) {
				quizDTO.setAnswerIndex(i);
				break;
			}
		}
		if (timelineQuizRespVo.getTotalNum() == null) {
			quizDTO.setTotalNum(0);
		} else {
			quizDTO.setTotalNum(timelineQuizRespVo.getTotalNum().intValue());
		}
		quizDTO.setOptionList(optionList);
		TimelineQuizInfoVo timelineQuizInfoVo = timelineQuizRespVo.getTimelineQuizInfoVo();
		quizDTO.setCorrectPicUrl(timelineQuizInfoVo.getQuizYPicUrl());
		quizDTO.setWrongPicUrl(timelineQuizInfoVo.getQuizNPicUrl());
		quizDTO.setCorrectDesc(timelineQuizInfoVo.getYesDesc());
		quizDTO.setWrongDesc(timelineQuizInfoVo.getNoDesc());
		quizDTO.setShowAnswerStartSec(timelineQuizInfoVo.gainQuizStartTime());
		quizDTO.setShowAnswerEndSec(timelineQuizInfoVo.gainQuizEndTime());
		quizDTO.setAnswerRelativePosition(timelineQuizInfoVo.getQuizRelativePosition());
		quizDTO.setLastSec(Collections.max(Arrays.asList(timelineQuizRespVo.gainEndTime(), timelineQuizInfoVo.gainQuizEndTime())));

		if (CollectionUtils.isNotEmpty(timelineQuizInfoVo.getQuizPointList())) {
			List<ItemPositionDTO> answerPositionList = (List<ItemPositionDTO>) CollectionUtils.collect(timelineQuizInfoVo.getQuizPointList(), new Transformer() {

				@Override
				public ItemPositionDTO transform(Object object) {
					PointPercentVo vo = (PointPercentVo) object;
					ItemPositionDTO result = new ItemPositionDTO();
					result.setPositionX(vo.getPointxPercent());
					result.setPositionY(vo.getPointyPercent());
					return result;
				}
			});
			quizDTO.setAnswerPositionList(answerPositionList);
		}

		return quizDTO;
	}

	private MulticameraDTO convertMulticameraDTO(TimelineMulticameraRespVo timelineMulticameraRespVo) {
		MulticameraDTO multicameraDTO = convertTimelineBaseDTO(timelineMulticameraRespVo, MulticameraDTO.class);
		if (CollectionUtils.isNotEmpty(timelineMulticameraRespVo.getExtraVoList())) {
			List<MulticameraVideoDTO> videoList = (List<MulticameraVideoDTO>) CollectionUtils.collect(timelineMulticameraRespVo.getExtraVoList(), new Transformer() {

				@Override
				public MulticameraVideoDTO transform(Object object) {
					TimelineExtraMulticameraVo vo = (TimelineExtraMulticameraVo) object;
					MulticameraVideoDTO result = convertMulticameraVideoDTO(vo);
					return result;
				}
			});
			multicameraDTO.setVideoList(videoList);
		}
		multicameraDTO.setPip(timelineMulticameraRespVo.getPip());
		multicameraDTO.setDefaultVideoId(timelineMulticameraRespVo.getDefaultId());
		return multicameraDTO;
	}

	private MulticameraVideoDTO convertMulticameraVideoDTO(TimelineExtraMulticameraVo vo) {
		MulticameraVideoDTO result = new MulticameraVideoDTO();
		result.setRid(vo.getRid());
		result.setVideoId(vo.getRelateVideoid());
		result.setTitle(vo.getVideoTitle());
		result.setDesc(vo.getVideoDesc());
		result.setCameraType(vo.getCameraTypeEnum().getCameraType());
		result.setDelayTime(vo.getDelayTime());
		result.setSortNum(vo.getExtraSortNum());
		result.setPicUrl(vo.getPicurl());
		result.setTimelineId(vo.getTlid());

		if (CollectionUtils.isNotEmpty(vo.getMulticameraPositionList())) {
			List<ItemPositionDTO> positionList = (List<ItemPositionDTO>) CollectionUtils.collect(vo.getMulticameraPositionList(), new Transformer() {

				@Override
				public ItemPositionDTO transform(Object object) {
					PointPercentVo vo = (PointPercentVo) object;
					ItemPositionDTO result = new ItemPositionDTO();
					result.setPositionX(vo.getPointxPercent());
					result.setPositionY(vo.getPointyPercent());
					return result;
				}
			});
			result.setPositionList(positionList);
		}
		return result;
	}

	private TimelineItemDTO convertTimelineItemDTO(TimelineGoodsRespVo timelineGoodsRespVo) {
		TimelineItemDTO timelineItemDTO = convertTimelineBaseDTO(timelineGoodsRespVo, TimelineItemDTO.class);
		timelineItemDTO.setItemId(timelineGoodsRespVo.getGoodsid());
		ItemDTO itemDetail = resourceService.getItemDetail(timelineGoodsRespVo.getShowid(), timelineGoodsRespVo.getGoodsid());
		timelineItemDTO.setItemName(itemDetail.getItemName());
		timelineItemDTO.setTitle(itemDetail.getItemName());
		timelineItemDTO.setRidList(timelineGoodsRespVo.getPointRelatedidList());
		return timelineItemDTO;
	}

	private TimelineHighlightDTO convertTimelineHighlightDTO(TimelineHighlightRespVo timelineHighlightRespVo) {
		TimelineHighlightDTO timelineHighlightDTO = convertTimelineBaseDTO(timelineHighlightRespVo, TimelineHighlightDTO.class);
		timelineHighlightDTO.setLastSec(timelineHighlightDTO.getStartSec());
		timelineHighlightDTO.setBubbleType(timelineHighlightRespVo.getBubbleKey());
		timelineHighlightDTO.setRid(timelineHighlightRespVo.getRid());
		return timelineHighlightDTO;
	}

	private TimelineItemLocationDTO convertTimelineItemLocationDTO(ViscoveryLocationAndTimeInfoVo vo) {
		TimelineItemLocationDTO result = new TimelineItemLocationDTO();
		result.setMatrixHeight(Double.parseDouble(vo.getH()));
		result.setMatrixWidth(Double.parseDouble(vo.getW()));
		result.setMatrixX(Double.parseDouble(vo.getX()));
		result.setMatrixY(Double.parseDouble(vo.getY()));
		if (CollectionUtils.isNotEmpty(vo.getAfterMovePoint())) {
			ItemPositionDTO position = new ItemPositionDTO();
			position.setPositionX(vo.getAfterMovePoint().get(0).getPointxPercent());
			position.setPositionY(vo.getAfterMovePoint().get(0).getPointyPercent());
			result.setPosition(position);
		}
		result.setRid(vo.getRid());
		if(StringUtils.isNotEmpty(vo.getRelatedid())) {
			result.setRelatedId(vo.getRelatedid());
		} else {
			result.setRelatedId("");
		}
		result.setTime(vo.getTime().toString());
		return result;

	}

	private <T extends TimelineBaseDTO> T convertTimelineBaseDTO(TimelineResp timelineResp, Class<T> destObj) {
		TimelineBaseDTO timeLineBaseDTO;
		try {
			timeLineBaseDTO = (TimelineBaseDTO) destObj.newInstance();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		timeLineBaseDTO.setType(timelineResp.getTlTypeEnum().toString());
		timeLineBaseDTO.setShowId(timelineResp.getShowid());
		timeLineBaseDTO.setTimelineId(timelineResp.getTlid());
		timeLineBaseDTO.setTitle(timelineResp.getTlTitle());
		timeLineBaseDTO.setVideoId(timelineResp.getVideoid());
		timeLineBaseDTO.setDesc(timelineResp.getTlDesc());
		timeLineBaseDTO.setAnimationEffects(timelineResp.getAnimationEffects());
		timeLineBaseDTO.setPicUrl(timelineResp.getPicurl());

		if (CollectionUtils.isNotEmpty(timelineResp.getPointPercentVoList())) {
			List<ItemPositionDTO> positionList = (List<ItemPositionDTO>) CollectionUtils.collect(timelineResp.getPointPercentVoList(), new Transformer() {

				@Override
				public ItemPositionDTO transform(Object object) {
					PointPercentVo vo = (PointPercentVo) object;
					ItemPositionDTO result = new ItemPositionDTO();
					result.setPositionX(vo.getPointxPercent());
					result.setPositionY(vo.getPointyPercent());
					return result;
				}
			});
			timeLineBaseDTO.setPositionList(positionList);
		}
		timeLineBaseDTO.setStartSec(timelineResp.gainStartTime());
		timeLineBaseDTO.setEndSec(timelineResp.gainEndTime());
		timeLineBaseDTO.setLastSec(timelineResp.gainEndTime());
		timeLineBaseDTO.setThumbnailCard(timelineResp.getThumbnailCard());
		timeLineBaseDTO.setRelativePosition(timelineResp.getRelativePosition());
		return (T) timeLineBaseDTO;
	}

	private <T extends TimelineResp> T convertTimelineBaseVo(TimelineBaseDTO timelineBaseDTO, Class<T> destObj) {
		TimelineResp timelineResp;
		try {
			timelineResp = (TimelineResp) destObj.newInstance();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		timelineResp.setShowid(timelineBaseDTO.getShowId());
		timelineResp.setTlid(timelineBaseDTO.getTimelineId());
		timelineResp.setTlTypeEnum(getTimelineTypeByCode(timelineBaseDTO.getType()));
		timelineResp.setTlTitle(timelineBaseDTO.getTitle());
		timelineResp.setVideoid(timelineBaseDTO.getVideoId());
		timelineResp.setTlDesc(timelineBaseDTO.getDesc());
		timelineResp.setAnimationEffects(timelineBaseDTO.getAnimationEffects());
		timelineResp.setPicurl(timelineBaseDTO.getPicUrl());
		if (CollectionUtils.isNotEmpty(timelineBaseDTO.getPositionList())) {
			List<PointPercentVo> positionList = (List<PointPercentVo>) CollectionUtils.collect(timelineBaseDTO.getPositionList(), new Transformer() {

				@Override
				public PointPercentVo transform(Object object) {
					ItemPositionDTO dto = (ItemPositionDTO) object;
					PointPercentVo result = new PointPercentVo();
					result.setPointxPercent(new BigDecimal(dto.getPositionX()).setScale(4, RoundingMode.HALF_UP).doubleValue());
					result.setPointyPercent(new BigDecimal(dto.getPositionY()).setScale(4, RoundingMode.HALF_UP).doubleValue());
					return result;
				}
			});
			timelineResp.setPointPercentVoList(positionList);
		}
		timelineResp.setStartSec(timelineBaseDTO.getStartSec() % 60);
		timelineResp.setStartMin(timelineBaseDTO.getStartSec() / 60);
		if(timelineBaseDTO.getEndSec() != null) {
			timelineResp.setEndSec(timelineBaseDTO.getEndSec() % 60);
			timelineResp.setEndMin(timelineBaseDTO.getEndSec() / 60);
		}
		timelineResp.setRelativePosition(timelineBaseDTO.getRelativePosition());
		timelineResp.setThumbnailCard(timelineBaseDTO.getThumbnailCard());
		return (T) timelineResp;
	}

	private TimelineType getTimelineTypeByCode(String code) {
		for (TimelineType e : TimelineType.values()) {
			if (e.toString().equals(code)) {
				return e;
			}
		}
		return null;
	}

	private String convertPopupPic(String mobilePopupPicUrl, String tvPopupPicUrl) {
		Map<String, JSONObject> popupMap = new HashMap<String, JSONObject>();
		if (StringUtils.isNotEmpty(mobilePopupPicUrl)) {
			JSONObject obj = new JSONObject();
			obj.put("picUrl", mobilePopupPicUrl);
			popupMap.put("mobile", obj);
		}
		if (StringUtils.isNotEmpty(tvPopupPicUrl)) {
			JSONObject obj = new JSONObject();
			obj.put("picUrl", tvPopupPicUrl);
			popupMap.put("tv", obj);
		}
		return JsonUtil.toJson(popupMap);
	}
}
